package com.winit.openapi.fsp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.fsp.spi.loans.LoanContractService;
import com.winit.fsp.spi.loans.LoanContractService.WaitPayPurchaseKey;
import com.winit.fsp.spi.loans.LoanService;
import com.winit.fsp.spi.loans.command.GetLoanContractByLoanIdCommand;
import com.winit.fsp.spi.loans.command.GetLoanLinesByLoanIdCommand;
import com.winit.fsp.spi.loans.command.GetWaitPayPurchaseCommand;
import com.winit.fsp.spi.loans.command.GetWaitPayPurchaseTipsCommand;
import com.winit.fsp.spi.loans.command.OnLineSignContractCommand;
import com.winit.fsp.spi.loans.vo.LoanContract;
import com.winit.fsp.spi.loans.vo.LoanLine;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 貸款单合同
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * hui.li2 	1.0  		2015-3-10 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/fsp/loansContract")
public class LoanContractController extends BaseController {

    @Resource
    private LoanContractService  loanContractService;

    @Resource
    private LoanService          loanService;

    /**
     * 提示日期
     */
    private static final Integer TIPS_DAY = 5;

    public void setLoanContractService(LoanContractService loanContractService) {
        this.loanContractService = loanContractService;
    }

    /**
     * 进入录入贷款单合同页面，初始化产品信息(贷款单明细)
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/initLoansLine")
    public String initLoansLine(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        GetLoanLinesByLoanIdCommand command = new GetLoanLinesByLoanIdCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            if (json.containsKey("loansNo")) {
                command.setFspLoansId(json.getLongValue("fspLoansId"));
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // TODO 需确定怎样获取产品信息和贷款单明细信息
        List<LoanLine> list = loanService.getLoansLinesByLoansId(command);
        if (list != null) {
            responseMsg.setData(list);
        }
        return SUCCESS;
    }

    /**
     * 在线签订fsp贷款单合同
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/onLineSignContract")
    public String onLineSignContract(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        OnLineSignContractCommand cCommand = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            cCommand = buildOnLineSignContractCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        loanContractService.onLineSignContract(cCommand);
        return SUCCESS;
    }

    /**
     * 查询fsp贷款单合同详情
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/getLoansContract")
    public String getLoansContract(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        GetLoanContractByLoanIdCommand cCommand = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            cCommand = buildGetLoansContractByLoansNoCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        LoanContract loansContract = loanContractService.getLoanContractByLoanId(cCommand);
        if (loansContract != null) {
            responseMsg.setData(loansContract);
        }
        return SUCCESS;
    }

    /**
     * 获取未支付采购款
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/getWaitPayPurchase")
    public String getWaitPayPurchase(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        GetWaitPayPurchaseCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new GetWaitPayPurchaseCommand();
            if (json.containsKey("partnerId")) {
                command.setPartnerId(json.getInteger("partnerId"));
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        Map<WaitPayPurchaseKey, Object> payments = loanContractService.getWaitPayPurchase(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("count", payments.get(WaitPayPurchaseKey.count));
        data.put("amount", payments.get(WaitPayPurchaseKey.amount));
        responseMsg.setData(data);

        return SUCCESS;
    }

    /**
     * 获取未支付采购款
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/getWaitPayPurchaseTips")
    public String getWaitPayPurchaseTips(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        GetWaitPayPurchaseTipsCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new GetWaitPayPurchaseTipsCommand();
            if (json.containsKey("partnerId")) {
                command.setPartnerId(json.getInteger("partnerId"));
            }
            command.setDay(TIPS_DAY);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Map<WaitPayPurchaseKey, Object> payments = loanContractService.getWaitPayPurchaseTips(command);
        if(payments != null && Integer.valueOf(payments.get(WaitPayPurchaseKey.count).toString()) != 0){
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("count", payments.get(WaitPayPurchaseKey.count));
        data.put("amount", payments.get(WaitPayPurchaseKey.amount));
        data.put("loanId", payments.get(WaitPayPurchaseKey.loanId));
        data.put("currentDate", payments.get(WaitPayPurchaseKey.currentDate));
        responseMsg.setData(data);
        }
        return SUCCESS;
    }
     

    /**
     * 封装在线签订贷款单合同命令
     * 
     * @param json
     * @return
     */
    private OnLineSignContractCommand buildOnLineSignContractCommand(JSONObject json) {
        OnLineSignContractCommand command = new OnLineSignContractCommand();
        if (json.containsKey("loansContractId")) {
            command.setLoansContractId(json.getLong("loansContractId"));
        }
        return command;
    }

    /**
     * 封装查询贷款单合同命令
     * 
     * @param json
     * @return
     */
    private GetLoanContractByLoanIdCommand buildGetLoansContractByLoansNoCommand(JSONObject json) {
        GetLoanContractByLoanIdCommand command = new GetLoanContractByLoanIdCommand();
        if (json.containsKey("loansId")) {
            command.setLoansId(json.getLongValue("loansId"));
        }
        return command;
    }
}
